package com.rockcent.mall;

import com.aliyun.oss.OSSClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ning.http.client.AsyncHttpClient;
import com.rockcent.common.BaseConfig;
import com.rockcent.common.common.RDruidDataSource;
import com.rockcent.mall.lib.AutoLoginInterceptor;
import com.rockcent.mall.lib.factory.ArticleFactory;
import com.rockcent.mall.lib.helper.BaseUserHelper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import javax.servlet.MultipartConfigElement;
import javax.sql.DataSource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Configuration
public class Config extends BaseConfig {

    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("10MB");
        factory.setMaxRequestSize("100MB");
        return factory.createMultipartConfig();
    }
    /**
     * 公共线程池
     *
     * @return
     */
    @Bean
    ExecutorService commonExecutorService() {
        return Executors.newFixedThreadPool(5);
    }

    @Bean
    ObjectMapper objectMapper() {
        return new ObjectMapper();
    }
    @Bean
    AsyncHttpClient asyncHttpClient() {
        return new AsyncHttpClient();
    }

    @Bean
    NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }
    /**
     * 数据库连接池
     * @return
     */
    @Bean
    public DataSource druidDataSource() {
        return RDruidDataSource.getInstance(prop.jdbcUrl, prop.jdbcUsername, prop.jdbcPassword);
    }

    @Bean
    public ArticleFactory articleFactory() {
        return new ArticleFactory();
    }

    @Bean
    public AuthInterceptor authInterceptor(){
        return new AuthInterceptor(prop.userToken);
    }

    @Bean
    public UserPermissionInterceptor userPermissionInterceptor(){
        return new UserPermissionInterceptor(prop.userToken);
    }

    /**
     * rest api 系统
     * retrofit http 客户端库
     *
     * @return
     */
    @Bean
    Retrofit retrofit() {
        // 添加自定义客户端请求
        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.addInterceptor(chain -> {
            Request original = chain.request();
            Request request = original.newBuilder()
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        });
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(JacksonConverterFactory.create())
                .baseUrl(prop.apiBaseUrl)
                .client(httpClientBuilder.build())
                .build();
        return retrofit;
    }

    /**
     * 阿里云 oss 服务
     * @return
     */
    @Bean
    OSSClient ossClient(){
        return new OSSClient(prop.aliyunOssEndpoint, prop.aliyunOssAccessKeyId, prop.aliyunOssaccessKeySecret);
    }

    /**
     * 配置文件
     */
    @Autowired
    private Prop prop;

}